package com.hy.study.practice.queue;

import java.util.Scanner;

/**
 * 底层 为数组的  循环队列
 */
public class CircleQueueDemo {
    public static void main(String[] args) {
        CircleQueue circleQueue = new CircleQueue();
        Scanner scanner = new Scanner(System.in);
        try {
            char type = ' ';
            while (true) {
                System.out.println("----------请选择----------");
                System.out.printf("a(add) s(show) g(get) h(head) e (exit)");
                type = scanner.next().charAt(0);

                switch (type) {
                    case 'a':
                        System.out.println("请输入一个数:");
                        int value = scanner.nextInt();
                        try {
                            circleQueue.add(value);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }

                        break;
                    case 'g':
                        try {
                            int i = circleQueue.get();
                            System.out.println(i);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 'h':
                        try {
                            int head = circleQueue.head();
                            System.out.println(head);
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 's':
                        try {
                            circleQueue.show();
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 'e':
                        System.out.println("程序退出");
                        System.exit(0);
                        break;
                    default:
                        break;
                }
            }
        } finally {
            scanner.close();
        }
    }
}

class CircleQueue {
    private int rear;
    private int front;
    private int[] arr;
    private int maxSize = 16;

    /**
     * 初始化两个构造函数
     *
     * @param maxSize
     */
    public CircleQueue(int maxSize) {
        this.rear = 0;
        this.front = 0;
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public CircleQueue() {
        this.rear = 0;
        this.front = 0;
        arr = new int[maxSize];
    }

    /**
     * 判断数组是否满了
     *
     * @return
     */
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    /**
     * 判断队列是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 向队列中添加一个元素
     *
     * @param value
     */
    public void add(int value) {
        if (isFull()) {
            throw new RuntimeException("队列已满无法添加");
        }
        arr[rear] = value;
        rear = (rear + 1) % maxSize;
    }

    /**
     * 从队列中获取数据
     *
     * @return
     */
    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列中数据为空,无法取出");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    /**
     * 获取头节点
     *
     * @return
     */
    public int head() {
        return arr[front];
    }

    /**
     * 获取队列的实际有效个数
     *
     * @return
     */
    public int size() {
        return (rear - front + maxSize) % maxSize;
    }

    /**
     * 遍历整个队列
     */
    public void show() {
        for (int i = front; i < size() + front; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }
}